import { Effect, Reducer } from 'umi';
import { getArchivesList } from '@/services';
import { IArticleItem } from '@/types';

export interface ArchivesModelState {
  archivesList: IArticleItem[]
  archivesLength: number,
}

export interface ArchivesModelType {
  namespace: 'archives';
  state: ArchivesModelState;
  effects: {
    getArchivesList: Effect;
    // getArticleCategoryList: Effect
  };
  reducers: {
    save: Reducer<ArchivesModelState>;
  };
}

const ArchivesModel: ArchivesModelType = {
  //等价于vuex的namespace   相当于redux的子reduce
  namespace: 'archives',
  //定义的状态
  state: {
    archivesList: [],
    archivesLength: 0,
  },
  //副作用 对应的异步操作，常指网络请求
  effects: {
    *getArchivesList({ payload }, { call, put }) {
      const result = yield call(getArchivesList);
      if (result.data) {
        yield put({
          type: "save",
          payload: {
            archivesList: result.data[0],
            archivesLength: result.data[1]
          }
        })
      }
    },
  },

  //同步操作
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default ArchivesModel;
